﻿using System;
using System.Windows.Forms;
using Sumen.Business;
using DevExpress.XtraSplashScreen;
using Sumen.Library.Enum;

using Sumen.Business.Permission;
using Sumen.Business.Presenter.Category;
using Sumen.Business.Presenter.Inventory;
using Sumen.Library.Froms;


namespace Sumen.GUI.Inventory
{
    public partial class Frm_General_Inward_Outward : XtraUserControlKira
    {
        private InventoryPresenter _inventoryPresenter;
        private StockPresenter _stockPresenter;


        #region Form

        public Frm_General_Inward_Outward()
        {
            InitializeComponent();
            FormCode = EnumFormCode.FrmGeneralInwardOutward;
            buttonsArray1.capQuyen(new Permission(FormCode.ToString(), ClsPublic.User.User_Username));
            dateBegin.DateTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
            dateEnd.DateTime = dateBegin.DateTime.AddMonths(1).AddMinutes(-1);
            _inventoryPresenter = new InventoryPresenter();
            _stockPresenter = new StockPresenter();


            LoadStock();
            gridControl1.LevelTree.Nodes.Add("Chi tiết", gridView2);
        }

        #endregion

        #region Grid event

        private void gridView1_MasterRowEmpty(object sender, DevExpress.XtraGrid.Views.Grid.MasterRowEmptyEventArgs e)
        {
            //  Customers c = (Customers)gridView1.GetRow(e.RowHandle);
            // e.IsEmpty = c.Orders.Count == 0;
        }

        private void gridView1_MasterRowGetRelationCount(object sender, DevExpress.XtraGrid.Views.Grid.MasterRowGetRelationCountEventArgs e)
        {
            e.RelationCount = 1;
        }

        private void gridView1_MasterRowGetRelationName(object sender, DevExpress.XtraGrid.Views.Grid.MasterRowGetRelationNameEventArgs e)
        {
            e.RelationName = "Chi tiết";
        }

        private void gridView1_MasterRowGetChildList(object sender, DevExpress.XtraGrid.Views.Grid.MasterRowGetChildListEventArgs e)
        {
            //var c = gridView1.GetRow(e.RowHandle);
            var code = gridView1.GetRowCellValue(e.RowHandle, colCode) + string.Empty;
            var reftype = gridView1.GetRowCellValue(e.RowHandle, colRefType) + string.Empty;
            if (reftype == "1")
                e.ChildList = new BindingSource(_inventoryPresenter.GetInwardDetails(code), "");
            if (reftype == "2")
                e.ChildList = new BindingSource(_inventoryPresenter.GetOutwardDetails(code), "");
        }



        #endregion

        #region Button event

        private void buttonsArray1_btnEventClose_click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void buttonsArray1_btnEventProduceReports_click(object sender, EventArgs e)
        {
            LoadForm();
        }

        #endregion


        #region Method

        public override void LoadForm()
        {
            gridControl1.DataSource = _inventoryPresenter.GetInwardOutward(lookUpStock.EditValue + string.Empty, dateBegin.DateTime, dateEnd.DateTime);
            LoadStock();
        }

        void LoadStock()
        {
            lookUpStock.Properties.DataSource = _stockPresenter.GetAll();
        }

        #endregion




    }
}